package org.jeecg.modules.hotel.belongToSkd.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.hotel.belongToSkd.entity.LwgyStudentBed;
import org.jeecg.modules.hotel.belongToSkd.entity.LwgyStudentHistory;
import org.jeecg.modules.hotel.belongToSkd.entity.ThirdStandardChange;
import org.jeecg.modules.hotel.belongToSkd.service.ThirdStandardChangeService;
import org.jeecg.modules.hotel.belongToSkd.service.ThirdStandardService;
import org.jeecg.modules.hotel.history.entity.LwgyHotelHistory;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @Description:标准计费表非常表专门用来同步标签计费相关的异动数据
 * 它依赖标准计费信息,如果床位价格或者是学生退宿调宿等发生变动,
 * 推送到山科大中间库(third_standard_change表)，频率是每天推送一次
 * @author:MJ-BaBa
 * @date: 2021.07.15
 */

@Slf4j
public class ThirdStandardChangeJob implements Job {

    @Autowired
    private ThirdStandardService thirdStandardService;

    @Autowired
    private ThirdStandardChangeService thirdStandardChangeService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("============进入定时器=============");
        long start1 = System.currentTimeMillis();
        /**查询学生住宿变动信息*/
        List<LwgyStudentHistory> stuChangeList = thirdStandardService.queryChangeStu();
        long start2 = System.currentTimeMillis();
        log.info("powerList  耗时：" + (start2 - start1));

        List<ThirdStandardChange> bedList = new ArrayList<>();

        if(stuChangeList != null && !stuChangeList.isEmpty()){
            stuChangeList.forEach(item -> {
                ThirdStandardChange thirdStandardChange = new ThirdStandardChange();
                java.util.Calendar calendar = java.util.Calendar.getInstance();
                int year = calendar.get(java.util.Calendar.YEAR);
                if(item.getBehavior().equals("change_bed")){
                    String beforeBed = item.getBeforeBed();
                    String afterBed = item.getAfterBed();
                    // 查询之前的床位价格
                    LwgyStudentBed lwgyStuBed1 = thirdStandardService.queryBedPrice(beforeBed);
                    // 查询之后的床位价格
                    LwgyStudentBed lwgyStuBed2 = thirdStandardService.queryBedPrice(afterBed);
                    thirdStandardChange.setPersonnelName(item.getStudentName());
                    thirdStandardChange.setPersonnelNo(item.getStudentNumber());
                    thirdStandardChange.setEducationalBackground(item.getStudentEducation());
                    thirdStandardChange.setChangeType("1");
                    thirdStandardChange.setStandardNo(lwgyStuBed1.getBedPrice());
                    thirdStandardChange.setTransStandardNo(lwgyStuBed2.getBedPrice());
                    thirdStandardChange.setChangeDate(lwgyStuBed2.getUpdateTime());
                    thirdStandardChange.setCreatedTime(lwgyStuBed2.getCreateTime());
                    thirdStandardChange.setLastUpdatedTime(lwgyStuBed2.getUpdateTime());
                } else if (item.getBehavior().equals("leave_bed")){
                    String beforeBed = item.getBeforeBed();
                    // 查询之前的床位价格
                    LwgyStudentBed lwgyStuBed3 = thirdStandardService.queryBedPrice(beforeBed);
                    thirdStandardChange.setPersonnelName(item.getStudentName());
                    thirdStandardChange.setPersonnelNo(item.getStudentNumber());
                    thirdStandardChange.setEducationalBackground(item.getStudentEducation());
                    thirdStandardChange.setChangeType("2");
                    thirdStandardChange.setStandardNo(lwgyStuBed3.getBedPrice());
                    thirdStandardChange.setTransStandardNo(null);
                    thirdStandardChange.setChangeDate(lwgyStuBed3.getUpdateTime());
                    thirdStandardChange.setCreatedTime(lwgyStuBed3.getCreateTime());
                    thirdStandardChange.setLastUpdatedTime(lwgyStuBed3.getUpdateTime());

                }
                thirdStandardChange.setTransStandardTypeNo("ZS");
                thirdStandardChange.setStandardTypeNo("ZS");
                thirdStandardChange.setTransCount(BigDecimal.valueOf(1));
                thirdStandardChange.setOldTransCount(BigDecimal.valueOf(1));
                thirdStandardChange.setPeriodType("11");
                thirdStandardChange.setPeriodYear(String.valueOf(year));
                thirdStandardChange.setPeriodSort("01");
                thirdStandardChange.setFeesPeriodYear(String.valueOf(year));
                thirdStandardChange.setFeesPeriodSort("01");
                bedList.add(thirdStandardChange);
            });
        }
        System.out.println();
        if(oConvertUtils.isNotEmpty(bedList) && bedList.size() > 0){
            thirdStandardChangeService.insertBedInfo(bedList);
        }
    }
}
